package com.apex.lottery.service.impl;

import com.apex.lottery.controller.param.CreatePrizeParam;
import com.apex.lottery.controller.param.PageParam;
import com.apex.lottery.dao.dataobject.AwardDO;
import com.apex.lottery.dao.mapper.AwardMapper;
import com.apex.lottery.service.AwardService;
import com.apex.lottery.service.PictureService;
import com.apex.lottery.service.dto.PageListDTO;
import com.apex.lottery.service.dto.PrizeDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName AwardServiceImpl
 * @Description
 * @Author ZJX
 * @Date 2025/4/17 15:03
 * @Version 1.0
 **/
@Service
public class AwardServiceImpl implements AwardService {
    @Autowired
    private PictureService pictureService;

    @Autowired
    private AwardMapper awardMapper;
    @Override
    public Long createPrize(CreatePrizeParam param, MultipartFile picFile) {
//        上传图片
        String fileName = pictureService.savePicture(picFile);

//        存储
        AwardDO prizeDO = new AwardDO();
        prizeDO.setName(param.getPrizeName());
        prizeDO.setDescription(param.getDescription());
        prizeDO.setImageUrl(fileName);
        prizeDO.setPrice(param.getPrice());
        awardMapper.insert(prizeDO);

        return prizeDO.getId();

    }

    @Override
    public PageListDTO<PrizeDTO> findPrizeList(PageParam param) {
        // 总量
        int total = awardMapper.count();

        // 查询当前页列表
        List<PrizeDTO> prizeDTOList = new ArrayList<>();
        List<AwardDO> prizeDOList = awardMapper.selectPrizeList(param.offset(), param.getPageSize());

        for (AwardDO prizeDO : prizeDOList) {
            PrizeDTO prizeDTO = new PrizeDTO();
            prizeDTO.setPrizeId(prizeDO.getId());
            prizeDTO.setName(prizeDO.getName());
            prizeDTO.setDescription(prizeDO.getDescription());
            prizeDTO.setImageUrl(prizeDO.getImageUrl());
            prizeDTO.setPrice(prizeDO.getPrice());
            prizeDTOList.add(prizeDTO);
        }

        return new PageListDTO<>(total, prizeDTOList);
    }
}
